<?php

class importexport_data_b2c_members {


    /**
     * 导出会员数据，修改数据库已有数据
     *
     * @params array $row 数据库中一条会员数据
     * @return array $row 修改过后的数据
     */
    public function get_content_row($row){

        if(!$this->title){
            $this->title = $this->get_title();
        }

        $pam_account = kernel::single('b2c_user_object')->get_pam_data('*',$row['member_id']);
        $row['email'] = $pam_account['email'];
        $data[0] = $row;
        return $data;
    }//end function

    public function check_continue(&$contents,$line){

        if ( count($contents) == 1 || ($contents[$line]['email'] && $contents[$line-1]['email'] && $contents[$line]['email'] == $contents[$line-1]['email']) )
        {
            return true;
        }
        else
        {
            array_pop($contents);
            return false;
        }
    }

    /**
     *将导入的数据转换为sdf
     *
     * @param array $contents 导入的一条会员数据
     * @param string $msg 传引用传出错误信息
     */
    public function dataToSdf($contents, &$msg){
        $regionsModel = app::get('ectools')->model('regions');

        $members = current($contents);

        //会员名称不能为空
		if(!$members['name']){
			$msg['error'] = app::get('importexport')->_('会员名称不能为空');
            return false;
		}
        if( !$members['email']){
            $msg['error'] = app::get('importexport')->_('会员名称为：').$members['name'].app::get('importexport')->_('的邮箱不能为空');
            return false;
        }

        //电话区号不能为空
        if($members['mobile'] && !$members['phone_setting']){
            $msg['error'] = app::get('importexport')->_('会员名称为：').$members['name'].app::get('importexport')->_('的电话区号不能为空');
            return false;
        }

		//密码不能为空
		if(!$members['pwd']){
			$msg['error'] = app::get('importexport')->_('会员名称为：').$members['name'].app::get('importexport')->_('的密码不能为空');
            return false;
		}

        //地区不能为空
		if(!$members['m_provice'] || !$members['m_city'] || !$members['m_distinct']){
			$msg['error'] = app::get('importexport')->_('会员名称为：').$members['name'].app::get('importexport')->_('的地区不能为空');
            return false;
		}else{
            $m_regions = $regionsModel->getList('*',array('local_name'=>$members['m_distinct'],'region_grade'=>'3'));
            if($m_regions[0]){
                $m_region_id = $m_regions[0]['region_id'];
            }else{
                $msg['error'] = app::get('importexport')->_('会员名称为：').$members['name'].app::get('importexport')->_('的地区不存在');
                return false;
            }
        }

        /*********   付款人信息 start    *********/
        //公司类型不能为空
		if(!$members['pay_type']){
			//$msg['error'] = app::get('importexport')->_('会员名称为：').$members['name'].app::get('importexport')->_('的公司类型不能为空');
            //return false;
		}else{
            $c_type = '0';
            switch($members['pay_type']){
                case 'ООО':
				case 'Юр. лицо':
                    $c_type = '0';
                    break;
                case 'ЗАО':
                    $c_type = '1';
                    break;
                case 'ИП':
                    $c_type = '2';
                    break;
                case 'Физическое лицо':
                    $c_type = '3';
                    break;
            }
        }

/*
        //公司名称不能为空
		if(!$members['pay_name']){
			$msg['error'] = app::get('importexport')->_('会员名称为：').$members['name'].app::get('importexport')->_('的公司名称不能为空');
            return false;
		}

        //税务局登记号不能为空
		if(!$members['pay_inn']){
			$msg['error'] = app::get('importexport')->_('会员名称为：').$members['name'].app::get('importexport')->_('的税务局登记号不能为空');
            return false;
		}
*/
        /*********   收货地址信息 start    *********/
		/*
        //收货地区不能为空
		if(!$members['a_provice'] || !$members['a_city'] || !$members['a_distinct']){
			$msg['error'] = app::get('importexport')->_('会员名称为：').$members['name'].app::get('importexport')->_('的收货地区不能为空');
            return false;
		}else{
            $a_regions = $regionsModel->getList('*',array('local_name'=>$members['a_distinct'],'region_grade'=>'3'));
            if(!$a_regions[0]){
                $msg['error'] = app::get('importexport')->_('会员名称为：').$members['name'].app::get('importexport')->_('的地区不存在');
                return false;
            }else{
                $a_region_id = $a_regions[0]['region_id'];
            }
        }

        //收货地址不能为空
		if(!$members['a_addr']){
			$msg['error'] = app::get('importexport')->_('会员名称为：').$members['name'].app::get('importexport')->_('的收货地址不能为空');
            return false;
		}
*/
        /*********   收货人信息 start    *********/
/*
        //收货人姓名不能为空
		if(!$members['con_name']){
			$msg['error'] = app::get('importexport')->_('会员名称为：').$members['name'].app::get('importexport')->_('的收货人姓名不能为空');
            return false;
		}

        //收货人手机不能为空
		if(!$members['con_phone_setting'] || !$members['con_mobile']){
			$msg['error'] = app::get('importexport')->_('会员名称为：').$members['name'].app::get('importexport')->_('的收货人手机不能为空');
            return false;
		}

        //收货人电话不能为空
		if(!$members['con_tel']){
			$msg['error'] = app::get('importexport')->_('会员名称为：').$members['name'].app::get('importexport')->_('的收货人电话不能为空');
            return false;
		}
*/
        $save_data['member'] = array(
            'pam_account' => array(
                'login_name' => $members['email'],
                'login_password' => $members['pwd'],
            ),
            'reg_style' => '0',
            'contact' => array(
                'phone' => array(
                    'mobile' => $members['mobile'],
                ),
            ),
            'name' => $members['name'],
            'area' => 'mainland:'.$members['m_provice'].'/'.$members['m_city'].'/'.$members['m_distinct'].':'.$m_region_id,
            'phone_setting' => $members['phone_setting'],
            'member_lv' => array(
                'member_group_id' => '1',
            ),
        );

        //付款人信息 
		if($members['pay_inn'] && $members['pay_name'])
		{
			$save_data['company'] = array(
				'c_type' => $c_type,
				'name' => $members['pay_name'],
			);
			if($members['pay_type'] == 'Физическое лицо'){
				$save_data['company']['inn_p'] = $members['pay_inn'];
			}else{
				$save_data['company']['inn'] = $members['pay_inn'];
			}
		}

        //收货地址信息 
		if($members['a_provice'] && $members['a_city'] && $members['a_distinct'])
		{
			$area = array('area_type'=>'mainland','sar'=>array($members['a_provice'],$members['a_city'],$members['a_distinct']),'id'=>$a_region_id);
			$save_data['addrs'] = array(
				'area' => $area,
				'addr' => $members['a_addr'],
			);
		}

        //收货人信息 
		if($members['con_name'] && ($members['con_mobile'] || $members['con_tel']))
		{
			$save_data['consignee'] = array(
				'name' => $members['con_name'],
				'phone_setting' => $members['con_phone_setting'],
				'phone' => array(
					'telephone' => $members['con_tel'],
					'mobile' => $members['con_mobile'],
				),
			);
		}

        $save_data['is_import'] = true;
        return $save_data;
    }

    public function get_title(){
        /*todo 翻译 */

        $title= array(
            "member_id"=>         app::get('b2c')->_('member_id'),
            "member_lv_id"=>     app::get('b2c')->_("member_lv_id"),
            "crm_member_id"=>    app::get('b2c')->_("crm_member_id"),
            "name"=>              app::get('b2c')->_("name"),
            "point"=>           app::get('b2c')->_('point'),
            "lastname"=>        app::get('b2c')->_('lastname'),
            "firstname"=>       app::get('b2c')->_('firstname'),
            "area"=>            app::get('b2c')->_("area"),
            "addr"=>            app::get('b2c')->_('addr'),
            "mobile"=>          app::get('b2c')->_("mobile"),
            "tel"=>             app::get('b2c')->_('tel'),
            "email"=>app::get('b2c')->_("email"),
            "zip"=>app::get('b2c')->_('zip'),
            "order_num"=>app::get('b2c')->_("order_num"),
            "refer_id"=>app::get('b2c')->_('refer_id'),
            "refer_url"=>app::get('b2c')->_('refer_url'),
            "b_year"=>app::get('b2c')->_('b_year'),
            "b_month"=>app::get('b2c')->_('b_month'),
            "b_day"=>app::get('b2c')->_('b_day'),
            "sex"=>app::get('b2c')->_("sex"),
            "addon"=>app::get('b2c')->_('addon'),
            "wedlock"=>app::get('b2c')->_("wedlock"),
            "education"=>app::get('b2c')->_('education'),
            "vocation"=>app::get('b2c')->_('vocation'),
            "interest"=>app::get('b2c')->_('interest'),
            "advance"=>app::get('b2c')->_("advance"),
            "advance_freeze"=>app::get('b2c')->_("advance_freeze"),
            "point_freeze"=>app::get('b2c')->_("point_freeze"),
            "point_history"=>app::get('b2c')->_("point_history"),
            "score_rate"=>app::get('b2c')->_('score_rate'),
            "reg_ip"=>app::get('b2c')->_("reg_ip"),
            "regtime"=>app::get('b2c')->_("regtime"),
            "state"=>app::get('b2c')->_("state"),
            "pay_time"=>app::get('b2c')->_('pay_time'),
            "biz_money"=>app::get('b2c')->_("biz_money"),
            "fav_tags"=>app::get('b2c')->_('fav_tags'),
            "custom"=>app::get('b2c')->_('custom'),
            "cur"=>app::get('b2c')->_("cur"),
            "lang"=>app::get('b2c')->_('lang'),
            "unreadmsg"=>app::get('b2c')->_("unreadmsg"),
            "disabled"=>app::get('b2c')->_("disabled"),
            "remark"=>app::get('b2c')->_('remark'),
            "remark_type"=>app::get('b2c')->_("remark_type"),
            "login_count"=>app::get('b2c')->_("login_count"),
            "experience"=>app::get('b2c')->_("experience"),
            "foreign_id"=>app::get('b2c')->_('foreign_id'),
            "resetpwd"=>app::get('b2c')->_('resetpwd'),
            "resetpwdtime"=>app::get('b2c')->_('resetpwdtime'),
            "member_refer"=>app::get('b2c')->_("member_refer"),
            "source"=>app::get('b2c')->_("source"),
            "user_type"=>app::get('b2c')->_("user_type"),
            "is_main"=>app::get('b2c')->_("is_main"),
            "business_id"=>app::get('b2c')->_("business_id"),
            "phone_setting"=>app::get('b2c')->_("phone_setting"),
            "qq"=>app::get('b2c')->_('qq'),
            "msn"=>app::get('b2c')->_('msn'),
            "skype"=>app::get('b2c')->_('skype'),
            "wangwang"=>app::get('b2c')->_('wangwang'),
            "obtained_point"=>app::get('b2c')->_("obtained_point"),
            "freezed_point"=>app::get('b2c')->_('freezed_point'),
            "cumulation_point"=>app::get('b2c')->_('cumulation_point'),
            "login_account"=>app::get('b2c')->_("login_account")
        );
        return $title;
    }

}
